const _clickMarker = Symbol('_clickMarker')
const _addPolygon = Symbol('_addPolygon')
const _addOtherPolygon = Symbol('_addOtherPolygon')
const _deleteNode = Symbol('_deleteNode')
let polylines = []
let polyEditor
let polygon
let markerPoint = []
class Amap {
  constructor (map, amap) {
    if (!map || !amap) {
      throw new Error('map or amap is required')
    }
    this.map = map
    this.amap = amap
    this.path = []
  }

  addTeamPoint (points) {  // 通过坐标以红点显示
    const {
      map,
      amap
    } = this
    var markerContent = "<div style='width:10px;height:10px;background:red;border-radius:50%'></div>"
    points.forEach(item => {
      const notMarker = new amap.Marker({
        position: item,
        content: markerContent
      })
      map.add(notMarker)
      notMarker.setLabel({
        offset: new amap.Pixel(-60, -20), // 修改label相对于maker的位置
        content: 123123141231
      })
      map.setFitView(notMarker)
    })
  }
  addPoint (lnglat, labelShow) {
    const {
      map,
      amap
    } = this
    map.remove(markerPoint)
    markerPoint = []
    const position = new amap.LngLat(lnglat.lng, lnglat.lat)
    const marker = new amap.Marker({
      position,
      // 以 icon 的 [center bottom] 为原点
      offset: new amap.Pixel(-13, -30)
    })
    if (labelShow === 1) {
      marker.setLabel({
        offset: new amap.Pixel(-60, -20), // 修改label相对于maker的位置
        content: 123123141231
      })
    }
    // 将 markers 添加到地图
    map.add(marker)
    markerPoint.push(marker)
    map.setFitView(marker)
  }

  startPoint (callback) {
    polygon.on('click', (e) => {
      this.addPoint(e.lnglat)
      callback(e.lnglat)
    })
  }

  setData ({
    path,
    point
  }) {
    const {
      amap
    } = this
    if (point) {
      this[_addPolygon](path)
      this.addPoint(new amap.LngLat(point[0], point[1]))
    } else {
      this[_addOtherPolygon](path)
    }
  }

  // 画圆
  circleDrawing ({
    point,
    deliverRange,
    labelShow
  }) {
    const {
      map,
      amap
    } = this
    const circle = new amap.Circle({
      center: [point[0], point[1]], // 圆心位置
      radius: deliverRange, // 半径
      fillColor: '#1791fc40',
      strokeColor: '#FF33FF'
    })
    this.addPoint(new amap.LngLat(point[0], point[1]), labelShow)
    circle.setMap(map)
  }

  selectionPoint (callback) {
    this.map.on('click', (e) => {
      this.addPoint(e.lnglat)
      callback(e.lnglat)
    })
  }

  setPoint (point) { // 设置地图标点
    const {
      amap
    } = this
    this.addPoint(new amap.LngLat(point[0], point[1]))
  }

  // 画线
  addPolyline (path) {
    const {
      map,
      amap
    } = this
    map.remove(polylines)
    polylines = []
    const polyline = new amap.Polyline({
      path,
      isOutline: true,
      outlineColor: '#ffeeff',
      borderWeight: 3,
      strokeColor: '#3366FF',
      strokeOpacity: 1,
      strokeWeight: 6,
      strokeStyle: 'solid',
      strokeDasharray: [10, 5],
      lineJoin: 'round',
      lineCap: 'round',
      zIndex: 50
    })
    polyline.setMap(map)
    polylines.push(polyline)
    // 缩放地图到合适的视野级别
    map.setFitView([polyline])
  }

  // 清除地图上所有的覆盖物
  clearMap () {
    this.map.clearMap()
  }

  // 开始编辑
  polyEditorStart () {
    if (polyEditor) {
      polyEditor.open()
    }
  }

  // 关闭编辑
  polyEditorClose (callback) {
    if (polyEditor) {
      polyEditor.close()
      callback(this.path)
    }
  }

  // 检查点是否在多边形内
  isPointInRing (point, path) {
    const {
      amap
    } = this
    if (polyEditor) {
      polyEditor.close()
    }
    const isPointInRing = amap.GeometryUtil.isPointInRing(point, path)
    return isPointInRing
  }

  autocomplete () { // 输入相关关键字，联想相关地点
    const {
      map,
      amap
    } = this
    console.log(amap)
    amap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'], () => {
      const autoOptions = {
        city: '广州', // 城市，默认全国
        input: 'pickerInput' // 使用联想输入的input的id
      }
      const autocomplete = new amap.Autocomplete(autoOptions)
      amap.event.addListener(autocomplete, 'select', (e) => {
        map.setCenter(e.poi.location)
      })
    })
  }

  geolocation () { // 获取当前位置
    const {
      map,
      amap
    } = this
    map.plugin('AMap.Geolocation', () => {
      const geolocation = new amap.Geolocation({
        enableHighAccuracy: true, // 是否使用高精度定位，默认:true
        timeout: 10000, // 超过10秒后停止定位，默认：无穷大
        maximumAge: 0, // 定位结果缓存0毫秒，默认：0
        convert: true, // 自动偏移坐标，偏移后的坐标为高德坐标，默认：true
        showButton: true, // 显示定位按钮，默认：true
        buttonPosition: 'LB', // 定位按钮停靠位置，默认：'LB'，左下角
        buttonOffset: new amap.Pixel(10, 20), // 定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
        showMarker: true, // 定位成功后在定位到的位置显示点标记，默认：true
        showCircle: true, // 定位成功后用圆圈表示定位精度范围，默认：true
        panToLocation: true, // 定位成功后将定位到的位置作为地图中心点，默认：true
        zoomToAccuracy: true // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
      })
      map.addControl(geolocation)
      geolocation.getCurrentPosition()
      amap.event.addListener(geolocation, 'complete', (e) => {
        console.log(e)
      }) // 返回定位信息
    })
  }

  [_clickMarker] (path, finishCallback) {
    if (path.length > 2) {
      this.clearMap()
      this[_addPolygon](path, finishCallback)
    }
  }

  [_deleteNode] (index, callback) {
    callback(index, this)
  }

  [_addPolygon] (path, finishCallback) { // 编辑构建多边形，可以通过点击的方式点与点连线构成多边形
    const {
      map,
      amap
    } = this
    polygon = new amap.Polygon({
      path,
      isOutline: true,
      borderWeight: 3,
      strokeColor: '#FF33FF',
      strokeWeight: 6,
      strokeOpacity: 0.2,
      fillOpacity: 0.4,
      // 线样式还支持 'dashed'
      fillColor: '#1791fc',
      zIndex: 50
    })
    polygon.setMap(map)
    if (finishCallback) {
      finishCallback(path)
    }
    map.plugin(['AMap.PolyEditor'], () => {
      polyEditor = new amap.PolyEditor(map, polygon)
      polyEditor.on('end', (event) => {
        this.path = event.target.getPath()
      })
    })
  }

  [_addOtherPolygon] (path) { // 绘制多边形图案
    const {
      map,
      amap
    } = this
    const otherPolygon = new amap.Polygon({
      path,
      isOutline: true,
      borderWeight: 3,
      strokeColor: '#FF33FF',
      strokeWeight: 6,
      strokeOpacity: 0.2,
      fillOpacity: 0.4,
      fillColor: '#1791fc',
      zIndex: 50
    })
    otherPolygon.setMap(map)
  }

  getLocalBylnlt (data) { // 返回经纬度和地址转换的对象
    const {
      map,
      amap
    } = this
    const geocoder = new amap.Geocoder({
      city: '010'
    })
    return geocoder
  }
}

export default Amap
